<?php
namespace dk\core;

class Config extends Jobject {
    
    use \dk\traits\Singleton;
    
    private $_props;
    
    public function loadFile($file, $values = null){
        $configs = include($file);
        if(isset($configs) && is_array($configs)){
            $this->_props = $configs;
        }
        if($values && is_array($values)){
            $this->_props = array_merge($this->_props, $values);
        }
    }

    /**
     * Sets a value in our hash table.
     *
     * @param key Name of the value in the hash table
     * @param value Value that we want to assign to the key '$key'
     */
    public function set( $key, $value = null ){
        if(!is_array($key)){
            if(strpos($key, '.')){
                $name = explode(".", $name, 2);
                $this->_props[$name[0]][$name[1]] = $value;
            }else{
                $this->_props[$key] = $value;
            }
        } else {
            foreach($key as $kname => $value){
                if(strpos($kname, '.')){
                    $name = explode(".", $kname, 2);
                    $this->_props[$name[0]][$name[1]] = $value;
                }else{
                    $this->_props[$kname] = $value;
                }
            }
        }
    }

    /**
     * Returns the value associated to a key
     *
     * @param key Key whose value we want to fetch
     * @param defaultValue value that we should return in case the one we're looking for
     * is empty or does not exist
     * @return Value associated to that key
     */
    public function get( $key, $defaultValue = null ){
        if(strpos($key, '.')){
            $name = explode(".", $name, 2);
            if(isset($this->_props[$name[0]][$name[1]]) ) {
                return $key->_props[$name[0]][$name[1]];
            }else{
                return $defaultValue;
            }
        } elseif(isset($this->_props[$key])) {
            return $this->_props[$key];
        }else{
            return $defaultValue;
        }
    }
}